import taobao_open
from taobao_open.api.base import TopException

from app import settings
from app.decorator import app_auth_hook
from app.service import tbk
from libs.app_helper import resp_success, resp_error
from libs.form import RawJsonForm

'''
物料精选
https://open.taobao.com/api.htm?docId=33947&docType=2
'''


# 物料精选
@app_auth_hook
def dg_optimus_material(request, auth):
    form = RawJsonForm(request.body)
    member_id = auth.get('member_id')
    data, err, sub_code = tbk.dg_optimus_material(form, member_id)
    if err is not None:
        kwargs = {}
        if sub_code is not None:
            kwargs.update({'sub_code': sub_code})
        return resp_error(err, **kwargs)
    return resp_success(data)


# 物料搜索
@app_auth_hook
def dg_material_optional(request, auth):
    form = RawJsonForm(request.body)
    keyword = form.get('q')
    member_id = auth.get('member_id')
    data, err, sub_code = tbk.dg_material_optional(form, member_id, keyword)
    if err is not None:
        kwargs = {}
        if sub_code is not None:
            kwargs.update({'sub_code': sub_code})
        return resp_error(err, **kwargs)
    return resp_success(data)


def rebate_order_get(request):
    try:
        fields = 'tb_trade_parent_id,tb_trade_id,num_iid,item_title,item_num,price,pay_price,seller_nick,seller_shop_title,commission,commission_rate,unid,create_time,earning_time'
        form = RawJsonForm(request.body)
        start_time = form.get('start_time', '')
        page_no = form.get_int('page_no', 1)
        page_size = form.get_int('page_size', 30)
        if start_time == '':
            start_time = '2022-01-25 09:00:00'
        req = taobao_open.api.TbkRebateOrderGetRequest()
        req.set_app_info(taobao_open.appinfo(settings.TBK_APP_KEY, settings.TBK_APP_SECRET))
        req.fields = fields
        req.start_time = start_time
        req.span = 3600
        req.page_no = page_no
        req.page_size = page_size
        resp = req.getResponse()
        result = resp.get('tbk_dg_material_optional_response')
    except TopException as e:
        if e.errorcode == 15 and e.subcode == '50001':
            return resp_success()
        else:
            print(e)
            if e.submsg:
                err = e.submsg
            else:
                err = '搜索商品异常'
            return resp_error(err)
    except Exception as e:
        err = '接口异常'
        print(err, e)
        return resp_error(err)
    return resp_success(**result)


@app_auth_hook
def query_goods(request, auth):
    form = RawJsonForm(request.body)
    keyword = form.get('q')
    member_id = auth.get('member_id')
    if keyword == '':
        data, err, sub_code = tbk.dg_optimus_material(form, member_id)
    else:
        data, err, sub_code = tbk.dg_material_optional(form, member_id, keyword)
    if err is not None:
        kwargs = {}
        if sub_code is not None:
            kwargs.update({'sub_code': sub_code})
        return resp_error(err, **kwargs)
    return resp_success(data)
